<?php

namespace App\Services\BatchMoveGoldProducts;

use App\Models\GoldRound;
use App\Models\GoldProduct;
use Illuminate\Database\Eloquent\Collection;

class BatchMoveGoldProducts
{
	protected int $rid;
	protected Collection $goldProducts;

	public function __construct(Collection $collection, int $rid)
	{
		$this->rid = $rid;
		$this->goldProducts = $collection;
	}

	protected function canBeMoved(GoldProduct $goldProduct): bool
	{
		if ($goldProduct->isLocking()) {
			return false;
		} elseif ($goldProduct->isDisabled()) {
			return false;
		} else {
			return true;
		}
	}

	public function move()
	{
		if ($this->goldProducts->isEmpty()) return;

		if (! GoldRound::find($this->rid)) return;

		foreach ($this->goldProducts as $goldProduct) {

			if (! $this->canBeMoved($goldProduct)) continue;

			$goldProduct->update(['gold_round_id' => $this->rid]);
		}
	}
}
